package Univers;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Stroke;
import java.awt.TexturePaint;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import Interface.PanelUnivers;


public class CPlanete  {
	
	public static final int TYPE_LEGENDE_RIEN = 0 ;
	public static final int TYPE_LEGENDE_INITIALES = 1 ;
	public static final int TYPE_LEGENDE_ENTIER = 2 ;
		
	int id;
	COrbite sonOrbite;
	String nom = "";
	float diametre ;
	Color remplissage;
	TexturePaint remplissageTexture;
	String texteAfficher = "";
	Stroke contour ;
	Font police = CPolices.FONT_NORMAL;
	private int typeLegende = 0;
	double angleSurOrbite;
	Ellipse2D.Double ellipse2D ;
	private PanelUnivers sonPanel;
	private boolean active = true; //par defaut planete non active
	
	public CPlanete(int id,PanelUnivers panel, String nom, float diametre, float x, float y,
			Color remplissage, String texteAfficher, Stroke contour, Font police, COrbite orbite) {
		super();		
	
		sonPanel = panel;
		ellipse2D = new Ellipse2D.Double();
		this.id = id;
		this.nom = nom;
		this.diametre = diametre;
		this.ellipse2D.x = x;
		this.ellipse2D.y = y;
		this.remplissage = remplissage;
		this.texteAfficher = texteAfficher;
		this.contour = contour;
		this.police = police;
		this.ellipse2D.height = diametre ;
		this.ellipse2D.width = diametre;
		this.angleSurOrbite = 0;
		this.sonOrbite = orbite;
		
	}
	
	public CPlanete(int id,PanelUnivers panel, String nom, float diametre, float x, float y,
			Color remplissage, String texteAfficher, Stroke contour, Font police,COrbite orbite, double anglesurOrbite) {
		super();
		
		sonPanel = panel;
		ellipse2D = new Ellipse2D.Double();
		this.id = id;
		this.nom = nom;
		this.diametre = diametre;
		this.ellipse2D.x = x;
		this.ellipse2D.y = y;
		this.remplissage = remplissage;
		this.texteAfficher = texteAfficher;
		this.contour = contour;
		this.police = police;
		this.ellipse2D.height = diametre;
		this.ellipse2D.width = diametre;
		sonOrbite = orbite;
		this.angleSurOrbite = anglesurOrbite;
		
	}
	
	public void draw(Graphics2D g2d, JPanel panel) {
	this.draw(g2d, panel,false);
	}
	
	
	private BufferedImage getTextureImage(String file) {
	    // Create the test image.
	    Image img = new ImageIcon(file).getImage();
	    BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
	    Graphics2D g2 = bi.createGraphics();
	    g2.drawImage (img,null,null);
	    return bi;
	  }
	
	public void draw(Graphics2D g2d, JPanel panel, boolean afficherLeTexte) {
		//contour :
		if (contour != null) {
			g2d.setStroke(contour);
			g2d.setStroke(new BasicStroke(1f));
			//g2d.setStroke(null);
		}
		else {
			//RAS
		}
	
		if (remplissage != null) {
			g2d.setPaint(remplissage);
			
		}

		if (police != null) {
			g2d.setFont(police);
			
		}
		
		else {
			//RAS
		}
		if ((texteAfficher != "") && (texteAfficher != null) && (typeLegende !=TYPE_LEGENDE_RIEN)) {
			String texteAfficherIci = texteAfficher;
			if (typeLegende == TYPE_LEGENDE_INITIALES)
				texteAfficherIci = texteAfficher.substring(0,3) + "...";
			g2d.drawString(texteAfficherIci, (int)this.ellipse2D.x, (int)this.ellipse2D.y);
		}
		
		g2d.fill(this.ellipse2D);
		g2d.draw(this.ellipse2D);
	}
	
	public void setCoord(double x, double y) {
		this.ellipse2D.x = x;
		this.ellipse2D.y = y;
	}
	
	public double getCoordX() {
		return this.ellipse2D.x;
	}

	public double getCoordY() {
		return this.ellipse2D.y;
	}
	
	public double getWidthEllipse() {
		return this.ellipse2D.width;
	}
	public double getHeightEllipse() {
		return this.ellipse2D.height;
	}
	private void setWidthEllipse(double width) {
		this.ellipse2D.width = width;
	}
	private void setHeightEllipse(double height) {
		this.ellipse2D.height= height;
	}
	public void setDiametreEllipse(double taille) {
		this.ellipse2D.width = taille;
		this.ellipse2D.height= taille;
	}

	public double getDiametreEllipse() {
		return this.ellipse2D.width;
	}

	public Ellipse2D.Double getEllipse2D() {
		return ellipse2D;
	}

	public String getNom() {
		return nom;
	}

	public String getTexteAfficher() {
		return texteAfficher;
	}

	public void setTexteAfficher(String texteAfficher) {
		this.texteAfficher = texteAfficher;
	}

	public COrbite getSonOrbite() {
		return sonOrbite;
	}

	public void setAngleSurOrbite(double angleSurOrbite) {
		this.angleSurOrbite = angleSurOrbite;
	}
	
	public CPlanete genererFantome() {
		Color transparence = new Color(
				this.remplissage.getRed(),
				this.remplissage.getGreen()
				,this.remplissage.getBlue(),
				100);
		return new CPlanete(this.id,this.sonPanel,this.nom, (float)(this.getDiametre()), (float)this.ellipse2D.x, (float)this.ellipse2D.y,transparence, this.texteAfficher, this.contour, this.police,this.sonOrbite, this.angleSurOrbite);
		
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setSonOrbite(COrbite sonOrbite) {
		this.sonOrbite = sonOrbite;
	}

	public float getDiametre() {
		return diametre;
	}
	public boolean isActive() {
		if (active) return true;
		else return false;
	}

	public int getTypeLegende() {
		return typeLegende;
	}

	public void setTypeLegende(int typeLegende) {
		this.typeLegende = typeLegende;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public void setRemplissage(Color remplissage) {
		this.remplissage = remplissage;
	}


	
}
